﻿var swfu;
var b = true;
var iCountText = 7;

$(document).ready(function () {
    var settings = {
        flash_url: "/Resource/Common/swfupload.swf", // Relative to this file
        upload_url: "/Services/Users.ashx?action=Upload&SsId=" + userId,
        file_size_limit: "3 MB",
        file_types: "*.jpg;*.gif;*.png;*.jpeg",
        file_types_description: "All Images",
        file_upload_limit: 100,
        file_queue_limit: 20,
        custom_settings: {
            progressTarget: "fsUploadProgress",
            cancelButtonId: "btnCancel"
        },
        debug: false,

        // Button settings
        button_image_url: "/images/button-flash.gif",
        button_width: "160",
        button_height: "22",
        button_placeholder_id: "spanButtonPlaceHolder",
        button_text: '<span class="button">Chọn tập tin <span class="buttonSmall">(3 MB 1 file)</span></span>',
        button_text_style: '.button { font-family: Arial;font-size: 12px;color:#FF0000; } .buttonSmall { font-size: 10pt; }',
        button_text_left_padding: 9,
        button_text_top_padding: 2,

        // The event handler functions are defined in handlers.js
        file_queued_handler: fileQueued,
        file_queue_error_handler: fileQueueError,
        upload_start_handler: uploadStart,
        upload_progress_handler: uploadProgress,
        upload_error_handler: uploadError,
        upload_complete_handler: uploadComplete,
        queue_complete_handler: queueComplete,
        upload_success_handler: uploadSuccess
    };

    swfu = new SWFUpload(settings);

    //alert("abc");
    $(".lnkLogin").fancybox({
        autoSize: false,
        beforeLoad: function () {
            this.width = 485;
            this.height = 350;
        },
        padding: 0,
        'type': 'iframe',
        'href': '/Account/frmLogin.aspx'
    });

    $(".lnkPassword").fancybox({
        autoSize: false,
        beforeLoad: function () {
            this.width = 485;
            this.height = 350;
        },
        padding: 0,
        'type': 'iframe',
        'href': '/Account/frmChangePassword.aspx'
    });

    $(".lnkRegisterMin").fancybox({
        autoSize: false,
        beforeLoad: function () {
            this.width = 500;
            this.height = 540;
        },
        padding: 0,
        'type': 'iframe',
        'href': '/Account/frmRegister.aspx'
    });

    $('.fancybox-buttons').fancybox({
        openEffect: 'none',
        closeEffect: 'none',

        prevEffect: 'none',
        nextEffect: 'none',

        closeBtn: false,

        helpers: {
            title: {
                type: 'inside'
            },
            buttons: {}
        },

        afterLoad: function () {
            this.title = 'Image ' + (this.index + 1) + ' of ' + this.group.length + (this.title ? ' - ' + this.title : '');

            // Add tweet button
            //this.title += '<a href="https://twitter.com/share" class="twitter-share-button" data-count="none" data-url="' + this.href + '" style="color:#000;">Tweet</a> ';

            // Add FaceBook like button
            this.title += '<br /><br /><iframe src="//www.facebook.com/plugins/like.php?href=' + this.href.replace("475x0__", "") + '&amp;layout=button_count&amp;show_faces=true&amp;width=500&amp;action=like&amp;font&amp;colorscheme=light&amp;height=23" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:23px;" allowTransparency="true"></iframe>';
        }
    });

    $("a.share").fancybox({
        padding: 0,
        autoSize: false,
        beforeLoad: function() {
            this.width = 780;
            this.height = 143;
            return checkShare();
        }
    });

    WriteTextArea();

    $(".txtUrl").each(function () {
        $(this).blur(function () {
            ChangeStatus(this, 0, "Đường dẫn đến file ảnh");
        });

        $(this).focus(function () {
            ChangeStatus(this, 1, "Đường dẫn đến file ảnh");
        });
    });

    $("#btnAddMore").click(function () {
        if (iCountText == 10) {
            $("#btnAddMore").attr("disabled", "disabled");
        } else {
            $("#textUrl").append("<input type=\"text\" name=\"txtUrl\" id=\"txtUrl_" + iCountText + "\" value=\"Đường dẫn đến file ảnh\" size=\"32\" onblur=\"javascript:ChangeStatus(this,0,'Đường dẫn đến file ảnh');\" onfocus=\"javascript:ChangeStatus(this,1,'Đường dẫn đến file ảnh');\">");
            iCountText++;
        }
    });

    $(".deleteimage").click(function () {
        DeleteImage();
    });

    //Tab Code OnClick
    $("#tabCode li a").click(function () {
        if ($(this).attr("rel") == "url") {
            $("#txtChooseImage_Url").fadeIn(1000);
            $("#txtChooseImage_HTMLCode").hide();
            $("#txtChooseImage_BBCode").hide();

            $("#tabCode li.url").removeClass("selected").addClass("selected");
            $("#tabCode li.bbcode").removeClass("selected");
            $("#tabCode li.htmlcode").removeClass("selected");
        } else if ($(this).attr("rel") == "bbcode") {
            $("#txtChooseImage_BBCode").fadeIn(1000);
            $("#txtChooseImage_Url").hide();
            $("#txtChooseImage_HTMLCode").hide();

            $("#tabCode li.url").removeClass("selected");
            $("#tabCode li.bbcode").removeClass("selected").addClass("selected");
            $("#tabCode li.htmlcode").removeClass("selected");
        } else if ($(this).attr("rel") == "htmlcode") {
            $("#txtChooseImage_HTMLCode").fadeIn(1000);
            $("#txtChooseImage_Url").hide();
            $("#txtChooseImage_BBCode").hide();

            $("#tabCode li.url").removeClass("selected");
            $("#tabCode li.bbcode").removeClass("selected");
            $("#tabCode li.htmlcode").removeClass("selected").addClass("selected");
        }
        return false;
    });

    //Tab Image OnClick
    //    $("#tabImage li a").click(function () {
    //        if ($(this).attr("rel") != "computer") {
    //            $("#UploadedPhoto").hide();
    //            $("#UploadedPhotoUser").fadeIn(500);
    //            $("#tabImage li.normal").removeClass('normal').addClass('selected');
    //            $("#tabImage li.first").removeClass('selected').addClass('normal');
    //        } else {
    //            $("#UploadedPhoto").fadeIn(500);
    //            $("#UploadedPhotoUser").hide();
    //            $("#tabImage li.selected").removeClass('selected').addClass('normal');
    //            $("#tabImage li.first").removeClass('normal').addClass('selected');
    //        }
    //    });

    //Tab Upload OnClick
    $("#tabUpload li a").click(function () {
        if ($(this).attr("rel") != "computer") {
            $("#divFromComputer").hide();
            $("#divFromUrl").fadeIn(500);
            $("#tabUpload li.normal").removeClass('normal').addClass('selected');
            $("#tabUpload li.first").removeClass('selected').addClass('normal');
        } else {
            $("#divFromComputer").fadeIn(500);
            $("#divFromUrl").hide();
            $("#tabUpload li.selected").removeClass('selected').addClass('normal');
            $("#tabUpload li.first").removeClass('normal').addClass('selected');
        }
    });

    //CheckBox Selected
    $("div.UploadActive input[type=checkbox]").click(function () {
        var divId = $(this).attr("id").replace("cboCheck_", "");
        if ($(this).attr("checked")) {
            ChooseImage(divId);
            $("#UploadActive_" + divId).addClass("selected");
        } else {
            //remove value textarea
            RemoveImage(divId);
            $("#UploadActive_" + divId).removeClass("selected");
        }
    });

    //Upload Active Onmouseover & Onmouseout
    $("div.UploadActive").mouseover(function () {
        var divId = $(this).attr("id").replace("UploadActive_", "");
        var strTemp = $("#txtHiddenId").val();
        var test = strTemp.indexOf($(this).attr("id"));

        if (test == -1) {
            strTemp += $(this).attr("id").toString() + "|";
            $("#txtHiddenId").val(strTemp);

            if ($("#divFloatImg_" + divId).html() == null) {
                var strOut = "";
                strOut = strOut.concat("<div id=\"divFloatImg_").concat(divId).concat("\" class=\"divFloatImg\"></div>");
                //append html to element
                $(this).append(strOut);
            }
            //alert(parseInt($("#divFloatImg_" + divId).offset().left));
            //check width div and browser
            if ((parseInt($("#divFloatImg_" + divId).offset().left) + parseInt($(".divFloatImg").css("width"))) >= ($("#UploadedPhoto").width() - 20)) {
                $("#divFloatImg_" + divId).css("left", -260);
            }
            //load image detail
            LoadImageDetail(divId, "divFloatImg_" + divId);
            //show div float
            $("#divFloatImg_" + divId).show();
        }
        $("#divFloatImg_" + divId).show();
    }).mouseout(function () {
        var divId = $(this).attr("id").replace("UploadActive_", "");
        $("#divFloatImg_" + divId).hide();
    });
});

function checkShare() {
    var b = false;
    for (var i = 0; i < $(".UploadActive .UploadSource INPUT[name=cboCheck]").length; i++) {
        if ($(".UploadActive .UploadSource INPUT[name=cboCheck]")[i].checked) {
            b = true;
            break;
        }
    }
    return b;
}



var ShowImage = function (uploadId) {
    var strOut = "";
    var uploadName = $("#txtUploadName_" + uploadId).val();
    var uploadBy = $("#txtUploadBy_" + uploadId).val();
    var strPath = strDomain + "/Files/" + uploadBy.split("-")[3] + "/";

    strOut = strOut.concat("<div id=\"UploadActive_").concat(uploadId).concat("\" class=\"UploadActive clearfix\">");
    strOut = strOut.concat("    <div class=\"UploadThumbImg\">");
    strOut = strOut.concat("        <a data-fancybox-group=\"button\" class=\"fancybox-buttons\" href=\"").concat(strPath + "475x0__" + uploadName).concat("\">");
    strOut = strOut.concat("            <img src=\"").concat(strPath + "130x0__" + uploadName).concat("\" width=\"130\" alt=\"").concat(uploadName).concat("\" />");
    strOut = strOut.concat("        </a>");
    strOut = strOut.concat("    </div>");
    strOut = strOut.concat("    <div class=\"UploadSource clearfix\">");
    strOut = strOut.concat("        <input type=\"checkbox\" name=\"cboCheck\" value=\"").concat(uploadId).concat("\" id=\"cboCheck_").concat(uploadId).concat("\" />");
    strOut = strOut.concat("    </div>");
    strOut = strOut.concat("</div>");
    document.write(strOut);
};

var RemoveImage = function (divId) {
    var breakline = "\n";
    var currentValue = $("#txtChooseImage_Url").val();
    if (currentValue.length == 0) {
        breakline = "";
    }
    $("#txtChooseImage_Url").val(currentValue.replace(breakline, "").replace($("#txtDirectLink_" + divId).val(), ""));

    currentValue = $("#txtChooseImage_BBCode").val();
    if (currentValue.length == 0) {
        breakline = "";
    }
    $("#txtChooseImage_BBCode").val(currentValue.replace(breakline, "").replace($("#txtBBCode_" + divId).val(), ""));

    currentValue = $("#txtChooseImage_HTMLCode").val();
    if (currentValue.length == 0) {
        breakline = "";
    }
    $("#txtChooseImage_HTMLCode").val(currentValue.replace(breakline, "").replace($("#txtHTMLCode_" + divId).val(), ""));

    if ($("#txtChooseImage_HTMLCode").length < 10) {
        ShowHideTabShare(false);
    }
};

var ChooseImage = function (divId) {
    var breakline = "\n";
    var currentValue = $("#txtChooseImage_Url").val();
    if (currentValue.length == 0) {
        breakline = "";
    }
    $("#txtChooseImage_Url").val(currentValue + breakline + $("#txtDirectLink_" + divId).val());

    currentValue = $("#txtChooseImage_BBCode").val();
    if (currentValue.length == 0) {
        breakline = "";
    }
    $("#txtChooseImage_BBCode").val(currentValue + breakline + $("#txtBBCode_" + divId).val());

    currentValue = $("#txtChooseImage_HTMLCode").val();
    if (currentValue.length == 0) {
        breakline = "";
    }
    $("#txtChooseImage_HTMLCode").val(currentValue + breakline + $("#txtHTMLCode_" + divId).val());
};

var WriteTextArea = function () {
    $("#divPhotoChoose").append("<textarea cols=\"5\" rows=\"3\" class=\"mb5 text12\" name=\"txtChooseImage_Url\" id=\"txtChooseImage_Url\" style=\"width: 741px;font-size: 12px;line-height: 15px;\" onclick=\"javascript:this.select();\"></textarea>");
    $("#divPhotoChoose").append("<textarea cols=\"5\" rows=\"3\" class=\"mb5 text12\" name=\"txtChooseImage_BBCode\" id=\"txtChooseImage_BBCode\" style=\"display:none;width: 741px;font-size: 12px;line-height: 15px;\" onclick=\"javascript:this.select();\"></textarea>");
    $("#divPhotoChoose").append("<textarea cols=\"5\" rows=\"3\" class=\"mb5 text12\" name=\"txtChooseImage_HTMLCode\" id=\"txtChooseImage_HTMLCode\" style=\"display:none;width: 741px;font-size: 12px;line-height: 15px;\" onclick=\"javascript:this.select();\"></textarea>");
    $("#divPhotoChoose").append("<span class=\"clearfix\" style=\"font-size: 10px;color:#CCC;\">* Mã BB Code và Html Code giúp bạn post ảnh lên blogs, diễn đàn.</span>");
};

var ShowHideTabShare = function (isShow) {
    if ($("#txtChooseImage_Url").val().length != 0) {
        if (isShow) {
            $("#tabCode").fadeIn(1000);
            $("#divPhotoChoose").fadeIn(1000);
            //$("#divPhotoChoose").fancybox();
        } else {
            $("#tabCode").fadeOut(1000);
            $("#divPhotoChoose").fadeOut(1000);
        }
    }
};

var LoadImageDetail = function (imgId, divLoading) {
    var uploadId = imgId;
    var uploadName = $("#txtUploadName_" + uploadId).val();
    var uploadBy = $("#txtUploadBy_" + uploadId).val();
    var strPath = strDomain + "/Files/" + uploadBy.split("-")[3] + "/" + uploadName;

    if ($("#" + divLoading).html() == null) {
        var strOut1 = "";
        strOut1 = strOut1.concat("<div id=\"divFloatImg_").concat(uploadId).concat("\" class=\"divFloatImg\"></div>");
        $("#UploadActive_" + uploadId).append(strOut1);
        if ((parseInt($("#divFloatImg_" + uploadId).offset().left) + parseInt($(".divFloatImg").css("width"))) >= (window.screen.width - 20)) {
            $("#divFloatImg_" + uploadId).css("left", -260);
        }
        $("#divFloatImg_" + uploadId).hide();
    }

    if ($("#" + divLoading).html().length == 0) {
        var strOut = "";
        strOut = strOut.concat("    <div class=\"col\"><span class=\"labelFrm\">URL</span><input type=\"text\" style=\"width:322px;\" id=\"txtDirectLink_").concat(uploadId).concat("\" name=\"txtDirectLink_").concat(uploadId).concat("\" onclick=\"javascript:this.select();\" value=\"\" /></div>");
        strOut = strOut.concat("    <div class=\"col\"><span class=\"labelFrm\">BBCode</span><input type=\"text\" style=\"width:322px;\" id=\"txtBBCode_").concat(uploadId).concat("\" name=\"txtBBCode_").concat(uploadId).concat("\" onclick=\"javascript:this.select();\" value=\"\" /></div>");
        strOut = strOut.concat("    <div class=\"col\"><span class=\"labelFrm\">HTML Code</span><input type=\"text\" style=\"width:322px;\" id=\"txtHTMLCode_").concat(uploadId).concat("\" name=\"txtHTMLCode_").concat(uploadId).concat("\" onclick=\"javascript:this.select();\" value=\"\" /></div>");
        $("#" + divLoading).append(strOut);
    }

    $("#txtDirectLink_" + uploadId).val(strPath);
    $("#txtBBCode_" + uploadId).val("[URL=" + strDomain + "][IMG]" + strPath + "[/IMG][/URL]");
    $("#txtHTMLCode_" + uploadId).val("<a href=\"" + strDomain + "\" target=\"blank\"><img src=\"" + strPath + "\" alt=\"Up.Meotom.Net upload ảnh miễn phí, nhanh không cần đăng ký\"/> </a><br/>");
};

var DeleteImage = function () {
    if (confirm("Bạn có chắc muốn xóa ?")) {
        $("div.UploadActive input[type=checkbox]").each(function () {
            if (this.checked) {
                //alert(this.id.replace("cboCheck_", ""));
                $.post("/Services/Users.ashx", { action: "DeleteImage", ImgId: this.id.replace("cboCheck_", "") }, function () { });
            }
        });
        reloadPage();
    } else {
        return false;
    }
    return false;
};

function ConfirmDelete() {
    var b = false;
    $("div.UploadActive input[type=checkbox]").each(function () {
        if (this.checked) {
            b = true;
        }
    });
    
    if (b) {
        if (confirm("Bạn có chắc muốn xóa ?")) {
            return true;
        }
    }
    return false;
}

var CheckAll = function (chk, b) {
    var i;
    var divId;
    if (b) {
        for (i = 0; i < chk.length; i++) {
            divId = chk[i].id.replace("cboCheck_", "");
            $("#UploadActive_" + divId).addClass("selected");

            LoadImageDetail(divId, "divFloatImg_" + divId);
            ChooseImage(divId);
            chk[i].checked = true;
        }
    } else {
        for (i = 0; i < chk.length; i++) {
            divId = chk[i].id.replace("cboCheck_", "");
            $("#UploadActive_" + divId).removeClass("selected");

            RemoveImage(divId);
            chk[i].checked = false;
        }
    }
};

var ChangeStatus = function (field, vType, vMsg) {
    if (vType == 1) {
        if (field.value == vMsg) { field.value = ''; }
    } else if (vType == 0) {
        if (field.value == '') { field.value = vMsg; }
    }
};

/*var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2779370-13']);
_gaq.push(['_trackPageview']);

(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();*/